<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use app\admin\model\Role;
use app\util\Util;
use app\admin\model\Admin;

/*
* 管理员
*/
class AdminController extends CommonController
{
    /*
    * 管理员页面
    */
    public function index()
    {
        $list = Admin::field('id, username, time,role_id')
            ->order('time asc')
            ->paginate(10);
        $role=Role::column('name','id');

        return view('index', ['list' => $list,'role'=>$role]);
    }

    /*
    * 添加
    */
    public function add()
    {
        $role=Role::all();
        return view('add',compact('role'));
    }

    /*
    * 添加
    */
    public function postInsert()
    {
        $all = input('post.');

        $username = $all['username'];
        $password = $all['password'];
        $role_id=$all['role_id'];

        $exist = Admin::where('username', '=', $username)->find();
        if (!is_null($exist)) {
            return Util::ajaxReturn(0, '账号已存在', null);
        }

        $salt = Util::generateSalt();
        $password = $salt . $password . $salt;

        $data = [
            'username' => $username,
            'password' => md5($password),
            'role_id'=>$role_id,
            'salt' => $salt,
            'time' => time(),
        ];
        
        $result = Admin::insert($data);
        if ($result) {
            return Util::ajaxReturn(1, '添加成功', null);
        } else {
            return Util::ajaxReturn(0, '添加失败', null);
        }
    }
    
    /*
    * 编辑
    */
    public function edit()
    {
        $id = input('param.id');
        $role=Role::all();
        $admin = Admin::where('id', '=', $id)->field('id, username,role_id')->find();
        return view('edit', ['admin' => $admin,'role'=>$role]);
    }

    /*
    * 修改
    */
    public function postSave()
    {
        $all = input('post.');

        $id = $all['id'];
        $username = $all['username'];
        $password = $all['password'];
        $role_id=$all['role_id'];
        $exist = Admin::where('username', '=', $username)->where('id', '<>', $id)->find();
        if (!is_null($exist)) {
            return Util::ajaxReturn(0, '账号已存在', null);
        }

        $data = [
            'username' => $username,
            'role_id'=>$role_id,
        ];

        if (strlen($password) > 0) {
            $salt = Util::generateSalt();
            $password = $salt . $password . $salt;

            $data['password'] = md5($password);
        }

        $result = Admin::where('id', '=', $id)->update($data);
        if ($result) {
            return Util::ajaxReturn(1, '修改成功', null);
        } else {
            return Util::ajaxReturn(0, '修改失败', null);
        }
    }

    /*
    * 删除
    */
    public function del()
    {
        $id = input('get.id');

        $result = Admin::where('id', '=', $id)->delete();
        if ($result) {
            return Util::ajaxReturn(1, '删除成功', null);
        } else {
            return Util::ajaxReturn(0, '删除失败', null);
        }
    }



}
